package com.pic4493.adapter;

import java.util.ArrayList;

import com.pic4493.app.R;
import com.pic4493.controls.CstSetHeightWidthImageView;
import com.pic4493.entities.EAlbum;
import com.ppcodes.imageloader.core.DisplayImageOptions;
import com.ppcodes.imageloader.core.ImageLoader;

import android.R.integer;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class AdpList_PicList extends BaseAdapter
{
   private DisplayImageOptions imgConfig = new DisplayImageOptions.Builder().cacheInMemory().cacheOnDisc()
    .showStubImage(R.drawable.image_loading_meddle)
    .showImageForEmptyUri(R.drawable.image_loading_meddle)
		 .bitmapConfig(Bitmap.Config.RGB_565).build();

   private Context mContext;
   private LayoutInflater mInflater;
   private ArrayList<EAlbum> mList;
   private OnClickEvent mClickEvent;

   public AdpList_PicList(Context context, OnClickEvent onClickEvent)
   {
	  mContext = context;
	  mInflater = LayoutInflater.from(context);
	  mClickEvent = onClickEvent;
   }

   public void addItem(ArrayList<EAlbum> _list)
   {
	  try
	  {
		 if (mList == null)
		 {
			mList = new ArrayList<EAlbum>();
		 }
		 for (EAlbum item : _list)
		 {
			mList.add(item);
		 }
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "AdpList_PicList_addItem(_list) " + e);
	  }
   }

   public void cleanData()
   {
	  mList = null;
   }

   @Override
   public int getCount()
   {
	  return mList.size();
   }

   @Override
   public Object getItem(int position)
   {
	  return mList.get(position);
   }

   @Override
   public long getItemId(int position)
   {
	  return 0;
   }

   @Override
   public View getView(int position, View convertView, ViewGroup parent)
   {
	  try
	  {
		 ViewHolder holder;
		 if (convertView == null)
		 {
			convertView = mInflater.inflate(R.layout.uipiclist_item, null);
			holder = new ViewHolder();
			holder.img = (CstSetHeightWidthImageView) convertView.findViewById(R.id.uipiclist_item_thumbnail);
			holder.txt = (TextView) convertView.findViewById(R.id.uipiclist_item_txt_num);
			convertView.setTag(holder);
		 }
		 else
		 {
			holder = (ViewHolder) convertView.getTag();
		 }

		 // holder.txt.setText(String.valueOf(mList.get(position).getImageCount()));
		 ImageLoader.getInstance().displayImage(mList.get(position).getCoverUrl(), holder.img, imgConfig);
		 holder.img.mHeight = mList.get(position).getCoverHeight();
		 holder.img.mWidth = mList.get(position).getCoverWidth();
		 holder.img.setTag(position);
		 holder.img.setOnClickListener(new View.OnClickListener()
		 {
			@Override
			public void onClick(View v)
			{
			   mClickEvent.Do(Integer.valueOf(v.getTag().toString()), v);
			}
		 });
		 return convertView;
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "AdpList_PicList_getView(position, convertView, parent) " + e);
	  }
	  return new View(mContext);
   }

   public interface OnClickEvent
   {
	  public void Do(int position, View v);
   }

   static class ViewHolder
   {
	  TextView txt;
	  CstSetHeightWidthImageView img;
   }
}
